package ktt;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that Bubonic's radar is sweeping the field properly. This test
 * assumes that the Bubonic's radar will alternate between 0 and 180 degrees.
 * Test robot is Walls.
 * 
 * @author Kurt Teichman
 *
 */
public class TestBubonicRadarSweep extends RobotTestBed {
  /** True if the robot is now hugging the left or right wall. */
  boolean robotOnWall = false;
  /** True if the radar has swept the top of the battlefield. */
  boolean radarHasSweptTop = false;
  /** True if the radar has swept the bottom of the battlefield. */
  boolean radarHasSweptBottom = false;

  
  /**
   * Specifies that SittingDuck and Bubonic are to be matched up in this test
   * case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Walls,ktt.Bubonic";
  }
  
  /**
   * This test runs for 1 round.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * After each turn, check to see if Bubonic is on the wall. If so, set the
   * corresponding flag and now check to see if the <code>currentRadarHeading</code>
   * is facing the top of the map or if it's facing the bottom of the map.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double currentRadarHeading;
    double xPos = robot.getX();
    //Checks to see if the robot is on either the left or right wall
    if (xPos < 40 || xPos > (width - 40)) {
      robotOnWall = true;
    }
    
    if (robotOnWall) {
      currentRadarHeading = Math.toDegrees(robot.getRadarHeading());
      if (currentRadarHeading == 0) {
        radarHasSweptTop = true;
      } 
      else if (currentRadarHeading == 180) {
        radarHasSweptBottom = true;
      }
    }
  }
  
  
  /**
   * After the battle, check to see that Bubonic's radar as swept the
   * top of the map and the bottom of the map. 
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Radar has swept top and bottom after hugging wall:",
        radarHasSweptTop && radarHasSweptBottom && robotOnWall);
  }

}
